<!--
Date: 2025-08-13
Copyright (c) 2025 CAX Conversion Project
-->
<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="用户名称" prop="operName" label-width="100">
              <el-input v-model="queryParams.operName" placeholder="请输入用户名称" clearable />
            </el-form-item>
            <el-form-item label="模块名称" prop="title" label-width="100">
              <el-select v-model="queryParams.title" placeholder="请选择" clearable>
                <el-option v-for="dict in moduleNames" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="操作类型" prop="businessType">
              <el-select v-model="queryParams.businessType" placeholder="请选择" clearable>
                <el-option v-for="dict in operateTypes" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="操作时间" style="width: 308px">
              <el-date-picker
                v-model="dateRange"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
              ></el-date-picker>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="hover">
      <template #header>
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button type="primary" @click="handleExport">导出Excel</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button type="primary">批量删除</el-button>
          </el-col> -->
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table ref="roleTableRef" v-loading="loading" :data="tableDatas">
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column label="序号" type="index" width="80" :index="indexMethod" />
        <el-table-column label="用户名称" prop="operName" min-width="150" />
        <el-table-column label="模块名称" prop="title" min-width="150" />
        <el-table-column label="操作类型" prop="businessType" min-width="150">
          <template #default="scope">
            {{ getLabelByValue(operateTypes, scope.row.businessType) }}
          </template>
        </el-table-column>
        <el-table-column label="IP" prop="operIp" min-width="120" />
        <el-table-column label="操作状态" prop="operStatus" min-width="150" />
        <el-table-column label="操作时间" align="center" width="180" prop="operTime">
          <template #default="scope">
            <span>{{ parseTime(scope.row.operTime) }}</span>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script setup name="ConversionRecordManagement" lang="ts">
import { sysLogList, exportData } from '@/api/caxDataConversionMajorComponent/systemLog/index';
import { optionType } from '@/api/caxDataConversionMajorComponent/type';
const router = useRouter();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const loading = ref(false);
const showSearch = ref(true);
const total = ref(0);
const dateRange = ref<[DateModelType, DateModelType]>(['', '']);
const queryFormRef = ref<ElFormInstance>();
const queryParams = reactive({
  operName: '',
  title: '',
  businessType: '',
  beginCreateTime: '',
  endCreateTime: '',
  pageNum: 1,
  pageSize: 10
});
const tableDatas = ref([]);
const moduleNames: Array<optionType> = [
  {
    value: 'CAD格式转换',
    label: 'CAD格式转换'
  },
  {
    value: 'CAE_INPUT格式转换',
    label: 'CAE_INPUT格式转换'
  },
  {
    value: 'CAE_OUTPUT格式转换',
    label: 'CAE_OUTPUT格式转换'
  },
  {
    value: '源文件管理',
    label: '源文件管理'
  },
  {
    value: '转换记录管理',
    label: '转换记录管理'
  }
];
const operateTypes: Array<optionType> = [
  {
    value: '0',
    label: '其他'
  },
  {
    value: '1',
    label: '新增'
  },
  {
    value: '2',
    label: '修改'
  },
  {
    value: '3',
    label: '删除'
  },
  {
    value: '4',
    label: '授权'
  },
  {
    value: '5',
    label: '导出'
  }
];
const indexMethod = (index) => {
  return (queryParams.pageNum - 1) * queryParams.pageSize + index + 1;
};
const getLabelByValue = (arr, value) => {
  const obj = arr.find((item) => item.value === String(value));
  if (obj) {
    return obj.label;
  }
  return value;
};
const getList = async () => {
  loading.value = true;
  if (dateRange.value?.length) {
    queryParams.beginCreateTime = String(dateRange.value[0]);
    queryParams.endCreateTime = String(dateRange.value[1]);
  } else {
    queryParams.beginCreateTime = '';
    queryParams.endCreateTime = '';
  }
  const res = await sysLogList(queryParams);
  tableDatas.value = res.rows;
  total.value = res.total;
  loading.value = false;
};
/**
 * 搜索按钮操作
 */
const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
};

/** 重置 */
const resetQuery = () => {
  dateRange.value = ['', ''];
  queryFormRef.value?.resetFields();
  handleQuery();
};
const downloadFile = (res, fileName) => {
  const blob = new Blob([res]);
  const objUrl = URL.createObjectURL(blob);
  const link = document.createElement('a');
  link.setAttribute('href', objUrl);
  link.setAttribute('download', fileName);
  link.style.display = 'none';
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
};
const handleExport = () => {
  const formData = new FormData();
  for (const key in queryParams) {
    formData.append(key, queryParams[key]);
  }
  exportData(formData).then((res) => {
    downloadFile(res, '系统日志导出.xlsx');
  });
};
onMounted(() => {
  getList();
});
</script>
<style lang="scss" scoped></style>
